/** 
 
*/
import React, { PureComponent } from 'react';
import { DATA_ENTRY, AdvancedSearch, codeType, ContentPackage, PrimaryGhostC } from '../../../comps';
import { Tabs } from 'antd';
import { connect } from 'dva';
import ManualJudgment from './manualJudgment';
import BondAndSecurities from './bondAndSecurities';
import 'moment/locale/zh-cn';
import SppiJudgmentBasis from './sppiJudgmentBasis';

const { TabPane } = Tabs;

class SppiTestRecord extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      searchData: null,
      superMan: localStorage.getItem('antd-pro-superMan') == 'true',
      tenant: null,
      exchangeCode: null,
      tradingCode: null,
      visible2: false,
      isfirst: true,
      initialValue: null,
    };
  }

  componentDidMount() {
    if (this.state.superMan) {
      // this.setState({
      //     searchData: {
      //         tenantId: this.state.tenant
      //     }
      // })
    } else {
      this.setState({
        searchData: {},
        tenant: localStorage.getItem('antd-pro-tenant'),
      });
    }
  }

  onTenantChange = (e) => {
    if (this.state.superMan && this.state.searchData == null) {
      this.setState({
        searchData: {
          tenantId: e.key,
        },
        initialValue: e.key,
      });
    }
    if (e) {
      this.setState({ tenant: e.key });
    }
  };

  onSecurityChange = (e) => {
    if (e) {
      const { exchangeCode, tradingCode } = e;
      this.setState({ exchangeCode, tradingCode });
    } else {
      this.setState({ exchangeCode: null, tradingCode: null });
    }
  };

  onQuery = (e) => {
    let params = {};
    if (e.tenant) {
      params.tenantId = this.state.tenant;
    }

    if (e.security) {
      params.exchangeCode = this.state.exchangeCode;
      params.tradingCode = this.state.tradingCode;
    }
    if (e.result) {
      params.result = e.result;
    }
    params.nowTime = new Date().getTime(); //时间戳
    this.setState({ searchData: params, formReset: false });
  };

  render() {
    const { tenant, searchData, superMan, visible2, initialValue } = this.state;
    const searchProps = {
      dataSource: [
        {
          dataIndex: 'tenant',
          title: '租户',
          type: DATA_ENTRY.TENANT,
          initialValue: initialValue,
          onChange: this.onTenantChange,
          allowClear: false,
          onRef: (e) => this.setState({ tenantRef: e }),
          noQueryForInit: true,
          firstInit: true,
        },
        {
          dataIndex: 'security',
          title: '证券代码',
          codeType: codeType.sppi,
          type: DATA_ENTRY.SECURITIESCODE,
          queryItems: { tenant: tenant },
          disabled: !tenant,
          onChange: this.onSecurityChange,
        },
        {
          dataIndex: 'result',
          title: 'SPPI测试结果',
          type: DATA_ENTRY.SELECT,
          dataSource: [
            { value: '1', title: '通过' },
            { value: '2', title: '不通过' },
          ],
        },
      ],
      onQuery: this.onQuery,
      onClear: (e) => {
        this.setState({ formReset: true, exchangeCode: null, tradingCode: null });
        if (superMan) {
          this.setState({ tenant: this.state?.searchForm?.getFieldValue('tenant') });
        }
      },
    };

    // 非超户时移除租户组件
    !superMan && searchProps.dataSource.shift();

    return (
      <ContentPackage
        title='SPPI测试记录'
        contentHtml={
          <div style={{ height: '100%' }}>
            <div style={{ marginBottom: '20px' }}>
              <AdvancedSearch {...searchProps} onRef={(ref) => this.setState({ searchForm: ref.props.form })} />
            </div>

            <div style={{ marginBottom: '20px', textAlign: 'right' }}>
              <PrimaryGhostC title='配置' onClick={() => this.setState({ visible2: true })}></PrimaryGhostC>
            </div>
            <div>
              <Tabs defaultActiveKey={'1'}>
                <TabPane tab='人工判断' key='1'>
                  <ManualJudgment searchData={searchData} formReset={this.state.formReset}></ManualJudgment>
                </TabPane>
                <TabPane tab='中债' key='2'>
                  <BondAndSecurities searchData={searchData} jrfs={1} formReset={this.state.formReset}></BondAndSecurities>
                </TabPane>
                <TabPane tab='中证' key='3'>
                  <BondAndSecurities searchData={searchData} jrfs={2} formReset={this.state.formReset}></BondAndSecurities>
                </TabPane>
              </Tabs>
            </div>
            <SppiJudgmentBasis
              visible={visible2}
              tenantId={this.state.searchData?.tenantId}
              onOk={() => this.setState({ visible2: false })}
              onCancel={() => this.setState({ visible2: false })}
            ></SppiJudgmentBasis>
          </div>
        }
      />
    );
  }
}
export default connect(({ sppiTestRecordM, loading }) => ({
  sppiTestRecordM,
  loading,
}))(SppiTestRecord);
